package com.learning.assignment_1;

import android.app.Activity;
import java.io.IOException;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class VideoActivity extends Fragment implements SurfaceHolder.Callback {
	private Camera camera;
	private SurfaceView surfaceView;
	SurfaceHolder surfaceHolder;
	boolean previewing = false;
	boolean recording = false;
	private View mView;
	MediaRecorder recorder;
	private int cameraid = 0;

	String stringPath = "/sdcard/samplevideo.3gp";

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.list_videos_tab);
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.video_tab, container, false);
		
		
		recorder = new MediaRecorder();
        //initRecorder();
		surfaceView = (SurfaceView)mView.findViewById(R.id.surfaceview);
		
		
		
		surfaceView.setMinimumWidth(mView.getWidth());
		surfaceView.setMinimumHeight(mView.getHeight()-100);
		surfaceHolder = surfaceView.getHolder();
		surfaceHolder.addCallback(this);	     		
		
		final ImageButton record = (ImageButton) mView.findViewById(R.id.startvideo);
		record.setOnClickListener(new OnClickListener() {
			@Override
			 public void onClick(View v) {
		        if (recording) {
		            recorder.stop();
		            recording = false;
		            previewing = true;
		            camera = Camera.open();
		    		camera.setDisplayOrientation(90);
		    		if (camera != null) {
		    			try {
		    				camera.setPreviewDisplay(surfaceHolder);
		    				camera.startPreview();
		    			} catch (IOException e) {
		    				// TODO Auto-generated catch block
		    				e.printStackTrace();
		    			}
		    		}

		            
		            record.setImageResource(R.drawable.camera);
		            // Let's initRecorder so we can record again
		            
		        } else {
		        	initRecorder();
		            prepareRecorder();
		        	record.setImageResource(R.drawable.stop_icon);
		        	previewing = false;
		        	recording = true;
		        	try{
	        		camera.stopPreview();
	        		camera.release();
		            recorder.start();
		        	} catch(Exception ex){
		        		Log.e("BAO", ex.getMessage());		        		
		        	}
		        }
		    }
		});
		
		final ImageButton switch_cam = (ImageButton) mView.findViewById(R.id.switchcamera);
//		switch_cam.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				try{
//				if(camera.getNumberOfCameras() == 1){
//					switch_cam.setVisibility(View.INVISIBLE);
//				}
//				else if(camera.getNumberOfCameras() >1){
//					if(cameraid == Camera.CameraInfo.CAMERA_FACING_FRONT){
//						cameraid = Camera.CameraInfo.CAMERA_FACING_BACK;
//					}
//					else{
//						cameraid = Camera.CameraInfo.CAMERA_FACING_FRONT;
//					}
//					camera.open(cameraid);
//				}}
//				catch(Exception ex){
//					camera.release();
//				}
//			}
//		});
		return mView;
	}

	private void initRecorder() {
        recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

        CamcorderProfile cpHigh = CamcorderProfile
                .get(CamcorderProfile.QUALITY_HIGH);
        recorder.setProfile(cpHigh);
        Time now = new Time();
        now.setToNow();
        recorder.setOutputFile("/sdcard/beheo_"+ now.toString().substring(0, 15) +".mp4");
        now.format("dd:mm:yyyy hh:mm:ss");
        recorder.setMaxDuration(50000); // 50 seconds
        recorder.setMaxFileSize(50000000); // Approximately 5 megabytes
    }

    private void prepareRecorder() {
    	
        recorder.setPreviewDisplay(surfaceHolder.getSurface());
        
        try {
            recorder.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();

        }
    }

   

    public void surfaceCreated(SurfaceHolder holder) {
    	//Open Camera
    	camera = Camera.open();
		camera.setDisplayOrientation(90);
		if (camera != null) {
			try {
				camera.setPreviewDisplay(surfaceHolder);
				camera.startPreview();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    	
    	
    	//End Open
    	
        prepareRecorder();
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
    	//Release Camera
    	if(camera != null & previewing){
    		camera.stopPreview();
    		camera.release();
    	}
    	if (recorder != null & recording){
	    	recorder.stop();
	        recorder.release();
    	}
    }
    
    public void onPause(){
    	super.onPause();
    	if(camera != null & previewing){
    		camera.stopPreview();
    		camera.release();
    	}
    	if (recorder != null & recording){
	    	recorder.stop();
	        recorder.release();
    	}
    }
    public void onDestroyView(){
    	super.onDestroyView();
    	if(camera != null & previewing){
    		camera.stopPreview();
    		camera.release();
    	}
    	if (recorder != null & recording){
	    	recorder.stop();
	        recorder.release();
    	}
    }
    
    
}
